package org.thirdpart.sms;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

public class SMSUtil{
	public static final String path=URLDecoder.decode(SMSUtil.class.getClassLoader().getResource("").getPath());
	public static final String webPath=SMSUtil.path.replace("\\","/").replace("/WEB-INF/classes","");
	static HttpClient client=new HttpClient();
	Logger logger=Logger.getLogger(getClass());
	static Properties properties=new Properties();
	
	public SMSUtil(){
		// TODO Auto-generated constructor stub
		init();
	}
	public void init(){
		// TODO Auto-generated method stub
		try{
			properties.load(new FileInputStream(path + this.getClass().getName().replace(".","/") + ".properties"));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
		}finally{
		}
	}
	public String send(String tel,String modelId,HashMap<String,String> param){
		String url=properties.getProperty("url","");
		org.apache.log4j.Logger.getLogger(getClass()).info("url:" + url);
		HashMap<String,Object> map=new HashMap<String,Object>();
		map.put("modueId",modelId);
		map.put("tel",tel);
		map.put("param",param);
		try{
			ArrayList<Object> list=new ArrayList<Object>();
			list.add(map);
			String json=new Gson().toJson(list);
			String base64=Base64.encodeBase64String(json.getBytes("utf-8"));
			String rsBase64=invokePostMethod(url,"",base64);
			String rs=new String(Base64.decodeBase64(rsBase64.getBytes("utf-8")));
			ArrayList<Object> list_rs=new ArrayList<Object>();
			list_rs=new Gson().fromJson(rs,list_rs.getClass());
			for(Object object:list_rs){
				System.out.println(object.getClass());
				if(object instanceof LinkedTreeMap){
					LinkedTreeMap<String,String> rsMap=(LinkedTreeMap<String,String>)object;
					String status=rsMap.get("status");
					String content=rsMap.get("content");
					rs=status + "###" + content;
				}
			}
			return rs;
		}catch(UnsupportedEncodingException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IOException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	public String invokePostMethod(String methodString,String auth,String info) throws UnsupportedEncodingException,IOException{
		org.apache.log4j.Logger.getLogger(getClass()).info("地址:" + methodString);
		org.apache.log4j.Logger.getLogger(getClass()).info("发送Post:" + info);
		// PostMethod post=new PostMethod("/LenovoPublicInterFace/servlet/QueryData");
		PostMethod method=new PostMethod(methodString);
		method.setRequestBody(info);
		// NameValuePair simcard=new NameValuePair("info",xml);
		method.setRequestHeader("content-type","application/json; charset=utf-8");
		method.setRequestHeader("Authorization","Basic " + auth);
		// org.apache.log4j.Logger.getLogger(getClass()).info("Authorization==" + "Basic " + auth);
		// method.setRequestHeader("pass","admin");
		// method.setRequestBody(new NameValuePair[]{simcard});
		method.getParams().setContentCharset("utf-8");
		client.executeMethod(method);
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine());
		// 打印结果页面
		String response=new String((method.getResponseBodyAsString() + "").getBytes("utf-8"));
		// 打印返回的信息
		org.apache.log4j.Logger.getLogger(getClass()).info("返回:" + response);
		method.releaseConnection();
		return response;
	}
	public static void main(String[] args){
		SMSUtil smsUtil=new SMSUtil();
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("code","123321");
		String rs=smsUtil.send("13508287512","vali",map);
		System.out.println(rs);
	}
}
